﻿#include "BrowserTableHandlerSubsystemFEStructure.h"
#include "OperatorsInterface/OperAbstractSubsystem.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractFESubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKFESubSysStructure.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include <QCheckBox>

namespace GUI
{
    void BrowserTableHandlerSubsystemFEStructure::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Enabled") << tr("Damping spectrum")
            << tr("Number of Elements") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerSubsystemFEStructure::updateData()
    {
        if (_tableWidget == nullptr) return;
        if (_feMgr == nullptr || _specMgr == nullptr)return;

        QList<Acoustics::FITKAbstractFESubSys*> list = _feMgr->getSubSysByType(
            Acoustics::FITKAbstractFESubSys::FEStructure);
        //FE结构数量
        const int n = list.size();
        _tableWidget->setRowCount(n);
        for (int i = 0; i < n; ++i)
        {
            //转换为SEA声腔
            Acoustics::FITKFESubSysStructure* fe = dynamic_cast<Acoustics::FITKFESubSysStructure*>(list.at(i));
            if (fe == nullptr) continue;

            //数据ID
            const int id = fe->getDataObjectID();

            _tableWidget->setItem(i, 0, new QTableWidgetItem(fe->getDataObjectName(), id));
            QCheckBox* box = new QCheckBox("");
            box->setChecked(fe->getEnableState());
            connect(box, &QCheckBox::stateChanged, this, [fe, id](int state) {
                if (state == Qt::Checked)  fe->setEnableState(true);
                else if (state == Qt::Unchecked)  fe->setEnableState(false);
                EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
                if (operGraph)operGraph->updateGraph(id);
            });
            _tableWidget->setCellWidget(i, 1, box);
            QString specName = "";
            if (fe->getDampingLossSpectraObj())specName = fe->getDampingLossSpectraObj()->getDataObjectName();
            _tableWidget->setItem(i, 2, new QTableWidgetItem(specName, id));
            QString eleNum = QString::number(fe->getElementsID().size());
            _tableWidget->setItem(i, 3, new QTableWidgetItem(eleNum, id));
            _tableWidget->setItem(i, 4, new QTableWidgetItem(fe->getNote(), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractSubsystem * BrowserTableHandlerSubsystemFEStructure::getSubsystemOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractSubsystem>("OperSubsystemFEStructure");
    }

    bool BrowserTableHandlerSubsystemFEStructure::enableAttriSet()
    {
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2) return false;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return false;
        //名称列
        const int colIndex = item->column();
        //频谱
        if (colIndex != 2 ) return false;

        return true;
    }
}